package com.ruoyi.web.controller.base;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.base.domain.BasPollutionsource;
import com.ruoyi.base.service.IBasPollutionsourceService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.base.domain.BasePipenetworkassociationtable;
import com.ruoyi.base.service.IBasePipenetworkassociationtableService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 管网关联关系(企业关联排口污水处理厂关联入河排污口)Controller
 * 
 * @author maliang
 * @date 2025-10-01
 */
@RestController
@RequestMapping("/base/pipenetworkassociationtable")
public class BasePipenetworkassociationtableController extends BaseController
{
    @Autowired
    private IBasePipenetworkassociationtableService basePipenetworkassociationtableService;
    @Autowired
    private IBasPollutionsourceService basPollutionsourceService;

    /**
     * 查询管网关联关系(企业关联排口污水处理厂关联入河排污口)列表
     */
    //@PreAuthorize("@ss.hasPermi('base:pipenetworkassociationtable:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasePipenetworkassociationtable basePipenetworkassociationtable)
    {
        startPage();
        List<BasePipenetworkassociationtable> list = basePipenetworkassociationtableService.selectBasePipenetworkassociationtableList(basePipenetworkassociationtable);
        return getDataTable(list);
    }

    /**
     * 导出管网关联关系(企业关联排口污水处理厂关联入河排污口)列表
     */
    //@PreAuthorize("@ss.hasPermi('base:pipenetworkassociationtable:export')")
    @Log(title = "管网关联关系(企业关联排口污水处理厂关联入河排污口)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasePipenetworkassociationtable basePipenetworkassociationtable)
    {
        List<BasePipenetworkassociationtable> list = basePipenetworkassociationtableService.selectBasePipenetworkassociationtableList(basePipenetworkassociationtable);
        ExcelUtil<BasePipenetworkassociationtable> util = new ExcelUtil<BasePipenetworkassociationtable>(BasePipenetworkassociationtable.class);
        util.exportExcel(response, list, "管网关联关系(企业关联排口污水处理厂关联入河排污口)数据");
    }

    /**
     * 获取管网关联关系(企业关联排口污水处理厂关联入河排污口)详细信息
     */
    //@PreAuthorize("@ss.hasPermi('base:pipenetworkassociationtable:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(basePipenetworkassociationtableService.selectBasePipenetworkassociationtableById(id));
    }

    /**
     * 新增管网关联关系(企业关联排口污水处理厂关联入河排污口)
     */
    //@PreAuthorize("@ss.hasPermi('base:pipenetworkassociationtable:add')")
    @Log(title = "管网关联关系(企业关联排口污水处理厂关联入河排污口)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasePipenetworkassociationtable basePipenetworkassociationtable)
    {
        return toAjax(basePipenetworkassociationtableService.insertBasePipenetworkassociationtable(basePipenetworkassociationtable));
    }

    /**
     * 修改管网关联关系(企业关联排口污水处理厂关联入河排污口)
     */
    //@PreAuthorize("@ss.hasPermi('base:pipenetworkassociationtable:edit')")
    @Log(title = "管网关联关系(企业关联排口污水处理厂关联入河排污口)", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BasePipenetworkassociationtable basePipenetworkassociationtable)
    {
        return toAjax(basePipenetworkassociationtableService.updateBasePipenetworkassociationtable(basePipenetworkassociationtable));
    }

    @GetMapping("/pollutionsource/{rhoutfallId}")
    public AjaxResult pollutionsourcelistbypwdid(@PathVariable("rhoutfallId") String rhoutfallid){
        List<BasPollutionsource> pollutionsourceList = new ArrayList<>();
        BasePipenetworkassociationtable pipenetworkassociationtableParam = new BasePipenetworkassociationtable();
        pipenetworkassociationtableParam.setRhoutfallid(rhoutfallid);
        List<BasePipenetworkassociationtable> pipenetworkassociationtableList = basePipenetworkassociationtableService.selectBasePipenetworkassociationtableList(pipenetworkassociationtableParam);
        if(!CollectionUtils.isEmpty(pipenetworkassociationtableList)){
            for (BasePipenetworkassociationtable pipenetworkassociationtable : pipenetworkassociationtableList) {
                if(StringUtils.isNotEmpty(pipenetworkassociationtable.getWspolluteid())){
                    BasPollutionsource pollutionsource = basPollutionsourceService.selectBasPollutionsourceById(pipenetworkassociationtable.getWspolluteid());
                    if(pollutionsource != null){
                        pollutionsourceList.add(pollutionsource);
                    }
                }
            }
        }
        return AjaxResult.success(pollutionsourceList);
    }
    /**
     * 删除管网关联关系(企业关联排口污水处理厂关联入河排污口)
     */
    //@PreAuthorize("@ss.hasPermi('base:pipenetworkassociationtable:remove')")
    @Log(title = "管网关联关系(企业关联排口污水处理厂关联入河排污口)", businessType = BusinessType.DELETE)
	@PostMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(basePipenetworkassociationtableService.deleteBasePipenetworkassociationtableByIds(ids));
    }
}
